<?php
namespace Model;
use Kuyuan\WqCore\Model\Model;
use Kuyuan\WqCore\util\Util;
use Tool\tool\Tool;

class RedModel extends Model
{
    protected $_fields = array(
        "id",
        "uniacid",
        "uid",
        "red_type",
        "money",
        "pay",
        "charge",
        "pay_type",
        "type",
        "send_coupon",
        "coupon",
        "red_form",
        "red_form_id",
        "number",
        "numbered",
        "title",
        "media",
        "question",
        "answer",
        "award",
        "time_type",
        "start_time",
        "end_time",
        "area_type",
        "area",
        "link",
        "create_time",
        "status"
    );

    protected $_relations = array(
        "answer" => array("choice_question", "answer_question"),
        "start_time" => array("time"),
        "end_time" => array("time")
    );

    /**
     * 红包问题答案
     * @param $_value
     * @param $_data
     * @return string
     */
    public function setAnswerAttr($_value, $_data)
    {
        $_answer = "";
        if ($_data["question"] == 1) {
            $_answer = json_encode($_data["choice_question"]);
        } else if ($_data["question"] == 2) {
            $_answer = json_encode($_data["answer_question"]);
        }
        return $_answer;
    }

    /**
     * 红包开始时间
     * @param $_value
     * @param $_data
     * @return int
     */
    public function setStartTimeAttr($_value, $_data)
    {
        $_start_time = 0;
        if ($_data["time_type"] == 2) {
            $_start_time = $_data["time"]["start"];
        }
        return $_start_time;
    }

    /**
     * 红包结束时间
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function setEndTimeAttr($_value, $_data)
    {
        $_end_time = 0;
        if ($_data["time_type"] == 2) {
            $_end_time = $_data["time"]["end"];
        }
        return $_end_time;
    }

    /**
     * 红包素材集合
     * @param $_value
     * @return mixed
     */
    public function setMediaAttr($_value)
    {
        return json_encode($_value);
    }

    /**
     * 红包创建时间
     * @return mixed
     */
    public function setCreateTimeAttr()
    {
        return TIMESTAMP;
    }

    // -----get-----

    /**
     * 获取红包拥有者用户头像
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function getOwnerHeaderAttr($_value, $_data)
    {
        return W7McMembersModel::where("uid", $_data["uid"])
            ->cache(!KUYUAN_DEBUG)
            ->value("avatar");
    }

    /**
     * 红包拥有者昵称
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function getOwnerNicknameAttr($_value, $_data)
    {
        return W7McMembersModel::where("uid", $_data["uid"])
            ->cache(!KUYUAN_DEBUG)
            ->value("nickname");
    }

    /**
     * 红包封面
     * @param $_value
     * @param $_data
     * @return mixed|string
     */
    public function getCoverAttr($_value, $_data)
    {
        $_cover = "";
        if ($_data["red_type"] != "default") {
            $_media_arr = json_decode($_data["media"], true);
            $_media_id = $_media_arr[0];
            $_cover = tomedia(MediaModel::where("id", $_media_id)
                ->cache(!KUYUAN_DEBUG)
                ->value("thumb"));
        }
        return $_cover;
    }

    /**
     * 视频红包获取视频链接
     */
    public function getVideoAttr($_value, $_data)
    {
        $_video = "";
        if ($_data["red_type"] === "video") {
            $_media = json_decode($_data["media"], true);
            return tomedia(MediaModel::where("id", $_media[0])
                ->cache(!KUYUAN_DEBUG)
                ->value("source"));
        }
        return $_video;
    }

    /**
     * 获取图片红包图片
     */
    public function getImagesAttr($_value, $_data)
    {
        $_images = array();
        if ($_data["red_type"] == "image") {
            $_media = json_decode($_data["media"], true);
            foreach ($_media as $_v) {
                $_images[] = tomedia(MediaModel::where("id",$_v)->value("source"));
            }
        }
        return $_images;
    }

    /**
     * 推广链接
     * @param $_value
     * @return mixed
     */
    public function getLinkAttr($_value,$_data)
    {
        $_link = array();
        if($_value) {
            $_link = LinkModel::field(array("name","link"))
                    ->where("id",$_value)
                    ->cache(!KUYUAN_DEBUG)
                    ->find();
        }
        return $_link;
    }

    /**
     * 红包素材库id数组
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function getMediaAttr($_value, $_data)
    {
        return json_decode($_value, true);
    }

    /**
     * 选择题
     */
    public function getChoiceQuestionAttr($_value,$_data)
    {
        $_answer = array("",0,"","");
        if($_data["question"] == 1) {
            $_answer = json_decode($_data["answer"],true);
        }
        return $_answer;
    }

    /**
     * 问答题
     */
    public function getAnswerQuestionAttr($_value,$_data)
    {
        $_answer = array("","");
        if($_data["question"] == 2) {
            $_answer = json_decode($_data["answer"],true);
        }
        return $_answer;
    }

    /**
     * 红包时间
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function getTimeAttr($_value,$_data)
    {
        $_time = array("start"=>0,"end"=>0);
        if($_data["start_time"]) $_time["start"] = $_data["start_time"];
        if($_data["end_time"]) $_time["end"] = $_data["end_time"];
        return $_time;
    }

    /**
     * 是否开启过该红包
     */
    public function getUnderAttr($_value,$_data)
    {
        $_under = "not";
        $_red_record_id = RedRecordModel::where("pid",0)
            ->where("red_id",$_data["id"])
            ->where("uid",Util::uid())
            ->value("id");
        if($_red_record_id) $_under = "success";
        return $_under;
    }

    /**
     * 红包title
     */
    public function getTitleAttr($_value)
    {
        return Tool::sensitiveWord($_value);
    }

    /**
     * 是否已经提交表单
     * @param $_value
     * @param $_data
     */
    public function getSubmitFormAttr($_value,$_data)
    {
        if(RedFormModel::where("red_id",$_data["id"])
            ->where("uid",Util::uid())
            ->value("id")) {
            return 1;
        }
        return 0;
    }

    /**
     * 是否为发红包的主人
     */
    public function getOwnerAttr($_value,$_data)
    {
        if(isset($_data["uid"])) {
            return $_data["uid"] == Util::uid() ? 1 : 0;
        }
    }

}